<?php


namespace app\controllers;

use app\models\EnergyLogs;
use app\models\Users;
use app\services\SendMsgServices;
use app\services\UsersService;
use app\tools\T;
use yii\helpers\Json;

class UsersController extends BaseController {
    public $layout = false;

    public function actionRegister() {
        $this->layout = false;
        return $this->render('register', ['parent_invite_code' => $this->Params['parent_invite_code']]);
    }


    public function actionGetPhoneCode() {
        if (strlen($this->Params['mobile']) != 11) {
            $this->outJson([], ERROR_CODE, '请输入正确手机号码');
        }

        $phoneCode = T::createPhoneCode();
        $content = "【萌猪乐园】您的验证码为：" . $phoneCode . "，该验证码5分钟内有效，请勿泄露于他人。";

        $SendMsgSer = new SendMsgServices();
        $SendMsgSer->setContent($content);
        $SendMsgSer->setMobile($this->Params['mobile']);

        $obj = simplexml_load_string($SendMsgSer->sendMsg(), "SimpleXMLElement", LIBXML_NOCDATA);
        $ResultArr = json_decode(json_encode($obj), true);
        if ($ResultArr['returnstatus']) {
            T::getRedis()->set($this->mobileKey . ':' . $this->Params['mobile'], $phoneCode);
            T::getRedis()->expire($this->mobileKey . ':' . $this->Params['mobile'], EXPIRE_TIME * 5);
            T::getRedis()->set($this->mobileKey . ':Result', Json::encode($ResultArr));
            $this->outJson([], SUCCESS_CODE, '短信发送成功');
        } else {
            $this->outJson([], ERROR_CODE, '短信发送失败');
        }
    }


    public function actionRegisters() {
//        $phoneCode = T::getRedis()->get($this->mobileKey . ':' . $this->Params['mobile']);
//        if (empty($phoneCode)) {
//            $this->outJson([], ERROR_CODE, '验证码未通过');
//        }
//
//        if ($phoneCode != $this->Params['phoneCode']) {
//            $this->outJson([], ERROR_CODE, '验证码未通过');
//        }
        if (strlen($this->Params['mobile']) != 11) {
            $this->outJson([], ERROR_CODE, '手机号格式错误');
        }

        if ($this->Params['password'] != $this->Params['confirmPassword']) {
            $this->outJson([], ERROR_CODE, '两次密码输入不一致');
        }

        $Users = Users::findOne(['mobile' => $this->Params['mobile'], 'status' => 1]);
        if (!empty($Users)) {
            $this->outJson([], ERROR_CODE, '你已经注册过了');
        }

        $UsersSer = new UsersService();
        $UsersSer->setParams('mobile', $this->Params['mobile']);
        $UsersSer->setParams('username', $this->Params['username']);
        $UsersSer->setParams('password', $this->Params['password']);
        $UsersSer->setParams('mark', $this->Params['confirmPassword']);
        $UsersSer->setParams('parent_invite_code', $this->Params['parent_invite_code'] ?? '');
//        T::getRedis()->set($this->tokenKey . ':' . date("Y-m-d H:i:s") . 'logs', Json::encode($this->Params));
        if ($UsersSer->addUsers()) {
            $Users = Users::findOne(['mobile' => $this->Params['mobile'], 'status' => 1]);
            $EnergyLogs = new EnergyLogs();
            $EnergyLogs->type = 2;
            $EnergyLogs->num = 50;
            $EnergyLogs->user_id = $Users->id;
            $EnergyLogs->ctime = date("Y-m-d H:i:s");
            $EnergyLogs->save();
            $this->outJson([], SUCCESS_CODE, '注册成功');
        } else {
            $this->outJson([], ERROR_CODE, '注册失败，请重试');
        }
    }

    public function actionLogin() {
        $this->layout = false;
        return $this->render('login');
    }

    public function actionToLogin() {
        $Users = Users::findOne([
            'mobile' => $this->Params['mobile'],
            'password' => md5(sha1($this->Params['password']) . MENG_TOKEN),
        ]);
        if (!empty($Users)) {
            if ($Users->status == 0) {
                $this->outJson([], ERROR_CODE, '您的账户已无效');
            } else if ($Users->status == -1) {
                $this->outJson([], ERROR_CODE, '由于您违规操作，此账号已被封禁');
            } else if ($Users->status == 1) {
                \Yii::$app->user->login($Users);
                $this->outJson([], SUCCESS_CODE, '登录成功');
            }
        } else {
            $this->outJson([], ERROR_CODE, '用户名或密码错误');
        }
    }

    public function actionForget() {
        return $this->render('forget');
    }

    public function actionForgetData() {
        $phoneCode = T::getRedis()->get($this->mobileKey . ':' . $this->Params['mobile']);
        T::getRedis()->set($this->mobileKey . ':Params', Json::encode($this->Params));
        if (empty($phoneCode)) {
            $this->outJson([], ERROR_CODE, '验证码未通过');
        }

        if ($phoneCode != $this->Params['phoneCode']) {
            $this->outJson([], ERROR_CODE, '验证码未通过');
        }

        if ($this->Params['password'] != $this->Params['confirmPassword']) {
            $this->outJson([], ERROR_CODE, '两次密码输入不一致');
        }

        $Users = Users::findOne(['mobile' => $this->Params['mobile'], 'status' => 1]);
        if (empty($Users)) {
            $this->outJson([], ERROR_CODE, '您还未注册');
        }

        $Users->password = md5(sha1($this->Params['password']) . MENG_TOKEN);
        $Users->mark = $this->Params['password'];
        $Users->utime = date("Y-m-d H:i:s");
        if ($Users->save()) {
            $this->outJson([], SUCCESS_CODE, '密码重置成功');
        } else {
            $this->outJson([], ERROR_CODE, '密码重置失败，请重试');
        }
    }
}